package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.anno.AutoFill;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DishMapper {

    /**
     * 根据分类id查询菜品数量
     *
     * @param categoryId
     * @return
     */
    @Select("select count(id) from dish where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);


    /**
     * 新增菜品
     *
     * @param dish
     */
    @AutoFill(OperationType.INSERT)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into dish values " +
            "(null,#{name},#{categoryId},#{price},#{image},#{description},#{status}," +
            "#{createTime},#{updateTime},#{createUser},#{updateUser})")
    void insert(Dish dish);


    /**
     * 分页查询菜品列表
     *
     * @param dto
     * @return
     */
    Page<DishVO> list(DishPageQueryDTO dto);

    /**
     * 删除菜品
     *
     * @param ids
     */
    void delete(List<Long> ids);

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    Dish selectById(Long id);

    /**
     * 修改菜品
     *
     * @param dish
     */
    @AutoFill(OperationType.UPDATE)
    void update(Dish dish);


    /**
     * 动态条件查询菜品
     *
     * @param dish
     * @return
     */
    List<Dish> seleteByCategoryId(Dish dish);

    /**
     * 根据套餐id查询菜品
     *
     * @param setmealId
     * @return
     */
    @Select("select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = #{setmealId}")
    List<Dish> getBySetmealId(Long setmealId);


    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    List<Dish> listDish(Dish dish);

    /**
     * 修改菜品的状态
     * @param categoryId
     * @param id
     */
    @Update("update dish set status = 1 - status where id=#{id}")
    void status(Long categoryId, Long id);
}
